/** Itty Bitty RPG Bar **/




YUI().use('*', function(Y) {
  
  YUI.namespace("ibrpg");

  ibrpg = {
    mydom: document.domain,
    data: {
      visits: 0,
      player: {
        name:"name",
        char_class: "warrior",
        atk: 5,
        def: 5,
        health: 100,
        mana: 100,
        gold: 0,
        level: 0,
        inventory: [
          {
            name: "sword",
            atk: 3,
            def: 1
            },
          {
            name: "shield",
            atk: 1,
            def: 4
          }  
        ]
      }
    },
    getStorage: function(){
      if(window.globalStorage){
        var storage = globalStorage[ibrpg.mydom];
        if(!storage.ibrpgDATA){
          storage.ibrpgDATA = Y.JSON.stringify(ibrpg.data)
        } else {
          Y.log(storage.ibrpgDATA);
          ibrpg.data = Y.JSON.parse(storage.ibrpgDATA.value);
        }
        return true;
      } else {
        return false;
      }
    },
    setStorage: function(){
      if(window.globalStorage){
        var storage = globalStorage[ibrpg.mydom];
        storage.ibrpgDATA = Y.JSON.stringify(ibrpg.data);
        return true;
      }else {
        return false;
      }
    },
    init: function () {
      Y.log("ibrpg initted");
      var view = Y.Node.get(".ibrpg");
      //v
       Y.log(ibrpg.data);
      if(ibrpg.getStorage()){
        ibrpg.data.visits +=1;
        ibrpg.setStorage()
      }
      Y.log(ibrpg.data);
      
      displayTxt = "<div><h3>Welcome to Itty Bitty RPG</h3></div>";
      displayTxt +="<div> You've been here "+ibrpg.data.visits+" times.</div>";
      displayTxt +="<div><button id='ibrpg-btn-savedata'>Save Data</button></div>";
      view.set("innerHTML", displayTxt);
      
      Y.on('click', function(e){
        Y.log('saving data')
        ibrpg.setStorage();
      }, '#ibrpg-btn-savedata');
    }
  }
  
  
    // Implementation code
    Y.log('YUI3 starting up');
    
    Y.on("event:ready", function(){
      ibrpg.init();
    });
    
    
    
    
    
});
